{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**将 `arr`转换为2行的2维数组。**\n",
    "- `arr = np.arange(10)`\n",
    "\n",
    "【知识点：数组的操作】\n",
    "- 如何改变数组的形状？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T02:47:23.972422Z",
     "start_time": "2020-09-06T02:47:23.965489Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.arange(10)\n",
    "\n",
    "# 方法1\n",
    "x = np.reshape(arr, newshape=[2, 5])\n",
    "print(x)\n",
    "# [[0 1 2 3 4]\n",
    "#  [5 6 7 8 9]]\n",
    "\n",
    "# 方法2\n",
    "x = np.reshape(arr, newshape=[2, -1])\n",
    "print(x)\n",
    "# [[0 1 2 3 4]\n",
    "#  [5 6 7 8 9]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**垂直堆叠数组a和数组b。**\n",
    "- `a = np.arange(10).reshape([2, -1])`\n",
    "- `b = np.repeat(1, 10).reshape([2, -1])`\n",
    "\n",
    "【知识点：数组操作】\n",
    "- 如何垂直叠加两个数组？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T02:48:08.620475Z",
     "start_time": "2020-09-06T02:48:08.614519Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "[[1 1 1 1 1]\n",
      " [1 1 1 1 1]]\n",
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]\n",
      " [1 1 1 1 1]\n",
      " [1 1 1 1 1]]\n",
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]\n",
      " [1 1 1 1 1]\n",
      " [1 1 1 1 1]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(10).reshape([2, -1])\n",
    "b = np.repeat(1, 10).reshape([2, -1])\n",
    "\n",
    "print(a)\n",
    "# [[0 1 2 3 4]\n",
    "#  [5 6 7 8 9]]\n",
    "print(b)\n",
    "# [[1 1 1 1 1]\n",
    "#  [1 1 1 1 1]]\n",
    "\n",
    "# 方法1\n",
    "print(np.concatenate([a, b], axis=0))\n",
    "# [[0 1 2 3 4]\n",
    "#  [5 6 7 8 9]\n",
    "#  [1 1 1 1 1]\n",
    "#  [1 1 1 1 1]]\n",
    "\n",
    "# 方法2\n",
    "print(np.vstack([a, b]))\n",
    "# [[0 1 2 3 4]\n",
    "#  [5 6 7 8 9]\n",
    "#  [1 1 1 1 1]\n",
    "#  [1 1 1 1 1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**8. 将数组a与数组b水平堆叠。**\n",
    "- `a = np.arange(10).reshape([2, -1])`\n",
    "- `b = np.repeat(1, 10).reshape([2, -1])`\n",
    "\n",
    "【知识点：数组的操作】\n",
    "- 如何水平叠加两个数组？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T02:48:41.235432Z",
     "start_time": "2020-09-06T02:48:41.229447Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "[[1 1 1 1 1]\n",
      " [1 1 1 1 1]]\n",
      "[[0 1 2 3 4 1 1 1 1 1]\n",
      " [5 6 7 8 9 1 1 1 1 1]]\n",
      "[[0 1 2 3 4 1 1 1 1 1]\n",
      " [5 6 7 8 9 1 1 1 1 1]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(10).reshape([2, -1])\n",
    "b = np.repeat(1, 10).reshape([2, -1])\n",
    "\n",
    "print(a)\n",
    "# [[0 1 2 3 4]\n",
    "#  [5 6 7 8 9]]\n",
    "print(b)\n",
    "# [[1 1 1 1 1]\n",
    "#  [1 1 1 1 1]]\n",
    "\n",
    "# 方法1\n",
    "print(np.concatenate([a, b], axis=1))\n",
    "# [[0 1 2 3 4 1 1 1 1 1]\n",
    "#  [5 6 7 8 9 1 1 1 1 1]]\n",
    "\n",
    "# 方法2\n",
    "print(np.hstack([a, b]))\n",
    "# [[0 1 2 3 4 1 1 1 1 1]\n",
    "#  [5 6 7 8 9 1 1 1 1 1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**将 `arr`的2维数组按列输出。**\n",
    "- `arr =  np.array([[16, 17, 18, 19, 20],[11, 12, 13, 14, 15],[21, 22, 23, 24, 25],[31, 32, 33, 34, 35],[26, 27, 28, 29, 30]])`\n",
    "\n",
    "【知识点：数组的操作】\n",
    "- 如何访问二维数组的全部元素，并按列输出？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[16 11 21 31 26 17 12 22 32 27 18 13 23 33 28 19 14 24 34 29 20 15 25 35\n",
      " 30]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr =  np.array([[16, 17, 18, 19, 20],[11, 12, 13, 14, 15],[21, 22, 23, 24, 25],[31, 32, 33, 34, 35],[26, 27, 28, 29, 30]])\n",
    "y = arr.flatten(order='F')\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**给定两个随机数组A和B，验证它们是否相等。**\n",
    "\n",
    "- `A = np.random.randint(0,2,5) B = np.random.randint(0,2,5)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.array([1,2,3])\n",
    "B = np.array([1,2,3])\n",
    "\n",
    "# Assuming identical shape of the arrays and a tolerance for the comparison of values\n",
    "equal = np.allclose(A,B)\n",
    "print(equal)\n",
    "\n",
    "# Checking both the shape and the element values, no tolerance (values have to be exactly equal)\n",
    "equal = np.array_equal(A,B)\n",
    "print(equal)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T03:15:03.024776Z",
     "start_time": "2020-09-06T03:15:03.019739Z"
    }
   },
   "source": [
    "**在给定的numpy数组中找到重复的条目（第二次出现以后），并将它们标记为True。第一次出现应为False。**\n",
    "\n",
    "- `a = np.random.randint(0, 5, 10)`\n",
    "\n",
    "【知识点：数组操作】\n",
    "- 如何在numpy数组中找到重复值？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T03:15:33.059248Z",
     "start_time": "2020-09-06T03:15:33.054241Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 3 0 2 4 2 2 2 2]\n",
      "[False  True False  True False False  True  True  True  True]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.random.seed(100)\n",
    "a = np.random.randint(0, 5, 10)\n",
    "print(a)\n",
    "# [0 0 3 0 2 4 2 2 2 2]\n",
    "b = np.full(10, True)\n",
    "vals, counts = np.unique(a, return_index=True)\n",
    "b[counts] = False\n",
    "print(b)\n",
    "# [False  True False  True False False  True  True  True  True]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**建立一个随机数在1-10之间的3行2列的数组，并将其转换成2行3列的数组。**\n",
    "\n",
    "【知识点：数组的操作】\n",
    "- 改变数组形状？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9 0]\n",
      " [3 3]\n",
      " [4 6]]\n",
      "[[9 0 3]\n",
      " [3 4 6]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "x = np.random.randint(10, size=[3, 2])\n",
    "print(x)\n",
    "y = np.reshape(x, [2,3])\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
